package cn.com.czpb.client.module.subscribe.view;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.event.PublishEventForPublisher;
import cn.com.czpb.client.model.subscribe.FocusUserDetailBean;
import cn.com.czpb.client.model.subscribe.IsfocusUserEntity;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.SPUtil;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/7/25.
 */

public class TopViewSubscribeDetail implements View.OnClickListener {
    private View rootView;
    private Activity mActivity;
    private ImageView img_subscribe_id;
    private ImageView img_unsubscribe_id;
    private TextView tv_introduce_id;
    private TextView tv_title_id;
    protected SPUtil spu;//
    private String currentUid;

    public static TopViewSubscribeDetail instance(Activity activity){
        return new TopViewSubscribeDetail(activity);
    }

    public TopViewSubscribeDetail(Activity activity){
        //
        spu =SPUtil.getInstance();
        this.mActivity = activity;
        initView(activity);
    }

    public View getView(){
        return rootView;
    }
    /**
     * @param context
     */
    private void initView(Activity context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        rootView = layoutInflater.inflate(R.layout.view_subscribedetail,null);
        tv_title_id = rootView.findViewById(R.id.tv_title_id);
        tv_introduce_id = rootView.findViewById(R.id.tv_introduce_id);
        img_subscribe_id = rootView.findViewById(R.id.img_subscribe_id);
        img_unsubscribe_id = rootView.findViewById(R.id.img_unsubscribe_id);
        img_unsubscribe_id.setOnClickListener(this);
        img_subscribe_id.setOnClickListener(this);
    }
    public void refreshData(FocusUserDetailBean bean){
        currentUid = bean.getUid();
        tv_title_id.setText(bean.getNickname());
        tv_introduce_id.setText(bean.getDesc());
        isFocus(bean.getUid());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_subscribe_id:

                if (spu.getUser() != null) {
                    LogUtils.i("quxiao ");
                    removeFocusUser(currentUid);
                    PublishEventForPublisher event = new PublishEventForPublisher();
                    event.setUid(currentUid);
                    event.setPublished(0);
                    EventBus.getDefault().post(event);
                } else {
                    PageCtrl.start2LoginActivity(mActivity);
                }
                break;
            case R.id.img_unsubscribe_id:
                if (spu.getUser() != null) {
                    LogUtils.i("dingyue");
                    focusUser(currentUid);
                    PublishEventForPublisher event2 = new PublishEventForPublisher();
                    event2.setUid(currentUid);
                    event2.setPublished(1);
                    EventBus.getDefault().post(event2);
                } else {
                    PageCtrl.start2LoginActivity(mActivity);
                }

                break;
        }
    }
    //判断是否订阅
    private void isFocus(String focusid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("device", DeviceUtils.getMyUUID(mActivity));
        map.put("focusid", focusid);
        if (spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        Factory.provideHttpService().isFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<IsfocusUserEntity>() {
                    @Override
                    public void call(IsfocusUserEntity isfocusUserEntity) {
                        setFocusData(isfocusUserEntity.data.getStatus());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }
    //订阅达人号
    private void focusUser(String uid) {
        Map<String, String> map = new HashMap<>();
        if (spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(mActivity));
        map.put("focusid", uid);
        Factory.provideHttpService().focusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        isFocus(currentUid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

    //取消订阅
    private void removeFocusUser(String uid) {
        Map<String, String> map = new HashMap<>();
        if (spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(mActivity));
        map.put("focusid", uid);
        Factory.provideHttpService().removeFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        isFocus(currentUid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }

    //设置状态
    private void setFocusData(String status) {
        LogUtils.i("status-->"+status);
        if ("0".equals(status)) {
            img_unsubscribe_id.setVisibility(View.VISIBLE);
            img_subscribe_id.setVisibility(View.GONE);
        } else {
            img_unsubscribe_id.setVisibility(View.GONE);
            img_subscribe_id.setVisibility(View.VISIBLE);
        }
    }

}
